import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:xinxuan/utils/color_util.dart';

class AppBarUtil extends StatelessWidget implements PreferredSizeWidget {

  @override
  final Size preferredSize;
  
  final String title;
  final double elevation;
  final Color? backgroundColor;
  final bool showLeading; 
  final List<Widget>? actions;
  final Color? shadowColor;
  final Color? titleColor;
  final PreferredSizeWidget? bottom;
  final double? toolbarHeight;
  final dynamic result;

  AppBarUtil({ 
    required this.title, 
    this.elevation = 0, 
    this.backgroundColor,
    this.showLeading = true,
    this.actions,
    this.shadowColor,
    this.titleColor,
    this.bottom,
    this.toolbarHeight,
    this.result
  }) : preferredSize =  Size.fromHeight(toolbarHeight ?? 44.h + (bottom?.preferredSize.height ?? 0.0));

  @override
  Widget build(BuildContext context) {
    return AppBar(
      leading: showLeading ? Builder(
        builder: (context) {
        return Center(
          child: IconButton(
            focusColor: Colors.transparent,
            hoverColor: Colors.transparent,
            splashColor: Colors.transparent,
            highlightColor: Colors.transparent,
            onPressed: () {
              Navigator.maybePop(context, result);
            }, 
            icon: Image.asset(
              'assets/icons/icon_back.png',
              width: 24.w,
              height: 24.w,
            )
          ),
        );
      }) : Container(),
      centerTitle: true,
      title: Text(
        title,
        style: TextStyle(
          fontSize: 20.sp,
          color: titleColor ?? textBlackColor,
          fontWeight: FontWeight.w400
        ),
      ),
      elevation: elevation,
      backgroundColor: backgroundColor,
      actions: actions,
      bottom: bottom,
    );
  }
}